package com.ruoyi.project.men.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.men.domain.MenContactUsDO;
import com.ruoyi.project.men.domain.MenFeedbackDO;
import com.ruoyi.project.men.service.MenContactUsService;
import com.ruoyi.project.men.service.MenFeedbackService;
import com.ruoyi.project.men.service.MenProtocolClassService;
import com.ruoyi.project.men.service.MenProtocolInfoService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务协议/文档中心/联系我们/意见反馈
 */
@Controller
@RequestMapping("/api/trotocol")
public class ApiTrotocolController {

    @Autowired
    private MenProtocolClassService menProtocolClassService;
    @Autowired
    private MenProtocolInfoService menProtocolInfoService;
    @Autowired
    private MenContactUsService menContactUsService;
    @Autowired
    private MenFeedbackService menFeedbackService;

    /***
     * 获取服务协议/文档中心分类
     * @param
     * @return
     */
    @PostMapping( "/getTrotocolClassList")
    @ResponseBody
    public R getTrotocolClassList(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("status") || StringUtils.isBlank(params.get("status").toString())

            ) {
                return R.error("参数错误");
            }
            return  menProtocolClassService.getTrotocolClassList(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 获取服务协议/文档详情
     * @param params classId : 分类id
     * @return
     */
    @PostMapping( "/getTrotocolInfoByClassId")
    @ResponseBody
    public R getTrotocolInfoByClassId(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("id") || StringUtils.isBlank(params.get("id").toString())

            ) {
                return R.error("参数错误");
            }
            return  R.ok().put("data",menProtocolInfoService.get(params.get("id").toString()));
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /***
     * 联系我们
     * @return
     */
    @PostMapping( "/getContactUs")
    @ResponseBody
    public R getContactUs(){

        try{

            Map<String,Object> map = new HashMap<>();
            map.put("delFlag","0");
            List<MenContactUsDO> list =menContactUsService.list(map);
            if(list.size()>0){
                return R.ok().put("data",list.get(0));
            }
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     *  意见反馈
     * @param params
     * @return
     */
    @PostMapping( "/saveFeedback")
    @ResponseBody
    public R saveFeedback(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("name")
                    ||!params.containsKey("phone")
                    || !params.containsKey("content")
                    || !params.containsKey("status")
                    || !params.containsKey("type")
                    || !params.containsKey("backId")

            ) {
                return R.error("参数错误");
            }
            MenFeedbackDO menFeedbackDO = new MenFeedbackDO();
            BeanUtils.populate(menFeedbackDO,params);
            menFeedbackDO.setCaAt(new Date());
            menFeedbackDO.setId(UuidUtil.get32UUID());
            menFeedbackDO.setDelFlag(0);
            menFeedbackService.save(menFeedbackDO);
            return R.ok();
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
}
